# developed by Xuan Yuan

class SymbolicDisjointSet:
    #member var: parent, sizeOfGroup
    def __init__(self, list):
        self.parent = {}
        self.sizeOfGroup = {}
        for item in list:
            self.parent[item] = item
            self.sizeOfGroup[item] = 1
    
    def join(self, A, B):
        rootA = self.getRoot(A)
        rootB = self.getRoot(B)
        if rootA == rootB:
            return
        if self.sizeOfGroup[rootA] < self.sizeOfGroup[rootB]:
            self.parent[rootA] = rootB
            self.sizeOfGroup[rootB] += self.sizeOfGroup[rootA]
        else:
            self.parent[rootB] = rootA
            self.sizeOfGroup[rootA] += self.sizeOfGroup[rootB]
    
    def getRoot(self, A):
        if self.parent[A] == A:
            return A
        return self.getRoot(self.parent[A])
    
    def size(self, A):
        root = self.getRoot(A)
        return self.sizeOfGroup[root]
